//
//  TitleView.m
//  BusinessMahjong
//
//  Created by JunMoMAC on 2016/11/14.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "TitleView.h"
#import "SelectTitleView.h"


#define kUIScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface TitleView ()<SelectTitleViewDelegate>
{
    UIButton *zonghe;
    UIButton *zhjiao;
    UIButton *jiage;
    UIButton *jgjiao;
    UIButton *xljiao;
    UIButton *xiaoliang;
    
    
}


/** 综合 */
@property (nonatomic , strong) SelectTitleView *synthesizeTitleView;
/** 价格 */
@property (nonatomic , strong) SelectTitleView *priceTitleView;
/** 销量 */
@property (nonatomic , strong) SelectTitleView *salesTitleView;

@end
@implementation TitleView


- (instancetype)initSelectViewWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)setZhongheType:(NSString *)zhongheType{
    
    _zhongheType = zhongheType;
    
    [zonghe setTitle:zhongheType forState:UIControlStateNormal];
}


- (void)createSubviews{
//    CGFloat width = (kUIScreenWidth - 60) / 3.0;
    CGFloat width = kUIScreenWidth  / 4;
    
    
    zonghe = [UIButton buttonWithType:UIButtonTypeCustom];
    zonghe.frame = CGRectMake(0, 0, width, self.frame.size.height);
    [zonghe setTitle:@"综合" forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self addSubview:zonghe];
    [zonghe addTarget:self action:@selector(zongAction:) forControlEvents:UIControlEventTouchUpInside];
    
    zhjiao = [UIButton buttonWithType:UIButtonTypeCustom];
    zhjiao.frame = CGRectMake(width-30, 15, 20, 15);
    [zhjiao setImage:[UIImage imageNamed:@"icon_arow_up@2x"] forState:UIControlStateNormal];
    [self addSubview:zhjiao];
    
    jiage = [UIButton buttonWithType:UIButtonTypeCustom];
    jiage.frame = CGRectMake(width, 0, width, self.frame.size.height);
    [jiage setTitle:@"销量" forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self addSubview:jiage];
    [jiage addTarget:self action:@selector(xiaoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    jgjiao = [UIButton buttonWithType:UIButtonTypeCustom];
    jgjiao.frame = CGRectMake(width*2-30, 15, 20, 15);
    [jgjiao setImage:[UIImage imageNamed:@"空"] forState:UIControlStateNormal];
    [self addSubview:jgjiao];
    
    xiaoliang = [UIButton buttonWithType:UIButtonTypeCustom];
    xiaoliang.frame = CGRectMake(2 * width, 0, width, self.frame.size.height);
    [xiaoliang setTitle:@"价格" forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self addSubview:xiaoliang];
    [xiaoliang addTarget:self action:@selector(jiaAction:) forControlEvents:UIControlEventTouchUpInside];
    xljiao = [UIButton buttonWithType:UIButtonTypeCustom];
    xljiao.frame = CGRectMake(width*3-30, 15, 20, 15);
    [xljiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];
    [self addSubview:xljiao];
    
    
    
    self.changeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.changeBtn.frame = CGRectMake(3 * width  , 0 , width, self.frame.size.height);
    // [self.changeBtn setBackgroundImage:[UIImage imageNamed:@"comlist_but_rank"] forState:(UIControlStateNormal)];
    //[self.changeBtn setBackgroundImage:[UIImage imageNamed:@"comlist_but_ranktwo"] forState:(UIControlStateSelected)];
    [self.changeBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [self.changeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.changeBtn addTarget:self action:@selector(changeMode:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.changeBtn];
    
    _shaixuanJiao = [UIButton buttonWithType:UIButtonTypeCustom];
    _shaixuanJiao.frame = CGRectMake(width*4-30, 15, 20, 15);
    [_shaixuanJiao setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    [self addSubview:_shaixuanJiao];
    
    
    
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
    bottomLineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:bottomLineView];
    
}

- (void)changeMode:(UIButton *)btn{//筛选
    btn.selected = !btn.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectView:changeBtn:)]) {
        [self.delegate selectView:self changeBtn:btn];
    }
    [_shaixuanJiao setImage:[UIImage imageNamed:@"shaixuan2"] forState:UIControlStateNormal];
    
//    [zhjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    [zonghe setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [jiage setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [jgjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    [xljiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    [xiaoliang setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.changeBtn setTitleColor:RGBAColor(62, 147, 253, 1) forState:UIControlStateNormal];
}
-(void)zongAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    [zonghe setTitleColor:RGBAColor(62, 147, 253, 1) forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
   [xljiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [jgjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    
    if (btn.selected) {
//        [zhjiao setImage:[UIImage imageNamed:@"sanjiao2"] forState:UIControlStateNormal];
        
       [self.delegate zonghe:self.zhongheType];
    }
    if (!btn.selected) {
//        [zhjiao setImage:[UIImage imageNamed:@"sanjiao"] forState:UIControlStateNormal];
        
        [self.delegate zonghe:self.zhongheType];

    }
    
    
}
-(void)jiaAction:(UIButton *)btn{//价格
    btn.selected = !btn.selected;
    if (btn.selected) {
        [xljiao setImage:[UIImage imageNamed:@"sanjiao2"] forState:UIControlStateNormal];
    }
    if (!btn.selected) {
        [xljiao setImage:[UIImage imageNamed:@"sanjiao"] forState:UIControlStateNormal];
    }
    [zhjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [jgjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    [xiaoliang setTitleColor:RGBAColor(62, 147, 253, 1) forState:UIControlStateNormal];
    
    
    [self.delegate jiage];
    
}
-(void)xiaoAction:(UIButton *)btn{//销量

    btn.selected = !btn.selected;
    if (btn.selected) {
        [jgjiao setImage:[UIImage imageNamed:@"sanjiao2"] forState:UIControlStateNormal];
    }
    if (!btn.selected) {
        [jgjiao setImage:[UIImage imageNamed:@"sanjiao"] forState:UIControlStateNormal];
    }
    
    [zhjiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [xljiao setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [jiage setTitleColor:RGBAColor(62, 147, 253, 1) forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [self.delegate xiaoliang];
}

#pragma mark -- SelectTitleViewDelegate
- (void)clickSelectTitleView:(SelectTitleView *)selectTitleView{


}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
